﻿$(document).ready(function () {

    $('.expand-all').click(function () {
        $('.dimensionheader h4 .toggle').attr('expanded', '0');
        $('.dimensionheader h4').click();
        return false;
    });

    $('.collapse-all').click(function () {
        $('.dimensionheader h4 .toggle').attr('expanded', '1');
        $('.dimensionheader h4').click();
        return false;
    });

    $('.dimensionheader h4').click(function () {
        var icon = $(this).children('img:first-child');
        if (icon.attr('expanded') == '1') {
            //Is expanded
            icon.attr('src', '/images/dim-left.png');
            icon.attr('expanded', '0');
            icon.closest('.dimensionheader').next().slideUp(200);
        } else {
            //Is collapsed
            icon.attr('src', '/images/dim-down.png');
            icon.attr('expanded', '1');
            icon.closest('.dimensionheader').next().slideDown(200);
        }
    });

    //Create tooltips for all placeholder fields
    $('[placeholder]').each(function () {
        $(this).attr('data-original-title', $(this).attr('placeholder'));
        $(this).attr('data-placement', 'auto');
        $(this).attr('data-toggle', 'tooltip');
        $(this).tooltip({ trigger: 'hover focus' });
        $(this).removeAttr('title');
    });

    $('[placeholder][required=true]').css('border', 'solid 1px #CF4415');

    $('.fadein').hide();
    $('.fadein').fadeIn();

    $(this).keyup(function (keyEvent) {
        if (keyEvent.keyCode == 27) {
            if ($('.modal').is(':visible')) {
                hideAllModals();
            }
        }
    });

    $('.detail-applied').click(function () {
        window.location = '/users/jobapplications.aspx';
        return false;
    });

    //Give all labels for check boxes a tooltip
    $('label[for]').each(function () {
        $(this).attr('title', $(this).text());
    });

    //Show the Apply button for multi-dimensional select
    $('.dimensionblock input[type=checkbox]').click(function () {
        if ($('.dimensionblock input[type=checkbox]:checked').length == 0) {
            $('.dimensionblock [buttontype=multi-dimension-apply][didx=' + $(this).attr('didx') + ']').hide();
        }
        else {
            $('.dimensionblock [buttontype=multi-dimension-apply][didx=' + $(this).attr('didx') + ']').fadeIn();
        }
    });

    //Actually apply a multi-dimensional select
    //Get all checked boxes NOT just the ones for this dimension in case they checked multiple dimensions
    $('.dimensionblock [buttontype=multi-dimension-apply]').click(function () {
        var values = $('.dimensionblock input[type=checkbox]:checked').map(function () {
            return $(this).attr('dvidx');
        }).get().join(',');

        $.ajax({
            type: 'POST',
            url: '/mainservice.asmx/generateurl',
            data: '{href: "' + window.location.href + '", values: "' + values + '"}',
            contentType: "application/json",
            dataType: "json",
            success: function (r) {
                window.location = r.d;
            },
            error: function () {
                //alert('An error occurred performing this action. 0x1719');
            }
        });
    });

});

function showMore(panelid, label) {
    $(label).fadeOut(100);
    $('#' + panelid).slideDown();
}

function toggleCollapse(name) {
    try {

        //actual div tag
        var collapsed = true;
        var t = document.getElementById(name);
        var element = $('#' + name);

        if (t != null) {
            if (t.collapsed == null) {
                t.collapsed = false;
            }
            else {
                t.collapsed = !t.collapsed;
            }
        }
        else return;

        //arrow image
        var img = document.getElementById(name + '_arrow');
        if (img != null) {
            if (t.collapsed == true)
                img.src = 'http://' + window.location.hostname + ':' + window.location.port + '/images/right_arrow.png';
            else
                img.src = 'http://' + window.location.hostname + ':' + window.location.port + '/images/down_arrow.png';
        }

        //list
        var l = document.getElementById(name + '_list');
        var elementList = $('#' + name + '_list');
        if (l != null) {
            if (t.collapsed)
                elementList.slideUp();
            else
                elementList.slideDown();
        }

    } catch (e) {
        alert(e.Message);
    }

}

function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2) ; i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}
